﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using AP.Framework.Infrastructure.Database;
using AP.Framework.Infrastructure.Repositories.IRepositories;
using AP.Framework.Infrastructure.Repositories;

namespace AP.Framework.Infrastructure.IoC
{
    public class SbResolver : IDependencyResolver
    {
        private UnityContainer container;
        public SbResolver()
        {
            this.container = new UnityContainer();
            //Instances to register
            ApContext context = new ApContext();
            //Database context singleton registration
            this.container.RegisterInstance(context);
            this.container.RegisterType<IModules, ModulesRepository>();
            this.container.RegisterType<IParam, ParamRepository>();
            this.container.RegisterType<IChannel, ChannelRepository>();
            this.container.RegisterType<IArticleCategory, ArticleCategoryRepository>();
            this.container.RegisterType<IProductCategory, ProductCategoryRepository>();
            this.container.RegisterType<IJobCategory, JobCategoryRepository>();
            this.container.RegisterType<IJob, JobRepository>();
            this.container.RegisterType<ILink, LinkRepository>();
            this.container.RegisterType<IProduct, ProductRepository>();
            this.container.RegisterType<IArticle, ArticleRepository>();
            this.container.RegisterType<IUsers, UsersRepository>();
            this.container.RegisterType<ICompanyProfile, CompanyProfileRepository>();
            this.container.RegisterType<IContact, ContactRepository>();
            //Register all controller type found in current assembly to the Unity container will be able to resolve them
            foreach (Type controllerType in (from t in Assembly.GetExecutingAssembly().GetTypes() where typeof(IController).IsAssignableFrom(t) select t))
            {
                this.container.RegisterType(controllerType);
            }
        }

        public object GetService(Type serviceType)
        {
            //
            //  Unity "Resolve" method throws exception so we need to wrap it up with non throwing method (like MVC expects)
            //
            return TryGetService(serviceType);
        }

        public object TryGetService(Type serviceType)
        {
            try
            {
                return this.container.Resolve(serviceType);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            //
            //  Unity "Resolve" method throws exception so we need to wrap it up with non throwing method (like MVC expects)
            //
            return TryGetServices(serviceType);
        }

        public IEnumerable<object> TryGetServices(Type serviceType)
        {
            try
            {
                return this.container.ResolveAll(serviceType);
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}